package com.talent.jiwen.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.talent.jiwen.R;
import com.talent.jiwen.base.BaseActivity;
import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.http.result.GroupInfoResult;
import com.talent.jiwen.http.result.InviteOrderResult;
import com.talent.jiwen.http.result.Member;
import com.talent.jiwen.ui.widgets.CourseInfoView;

import com.talent.jiwen.util.MaterialDialogUtil;
import com.talent.jiwen.util.SPConstant;
import com.talent.jiwen.util.SPUtil;
import com.talent.jiwen.util.Validators;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import pl.droidsonroids.gif.GifImageView;
import rx.Observable;

public class InviteDetailActivity extends BaseActivity {
    @BindView(R.id.courseInfoView)
    CourseInfoView courseInfoView;
    @BindView(R.id.teachTimeTv)
    TextView teachTimeTv;
    @BindView(R.id.acceptTv)
    TextView acceptTv;
    @BindView(R.id.refuseTv)
    TextView refuseTv;
    @BindView(R.id.loadingView)
    GifImageView loadingView;
    @BindView(R.id.contentLayout)
    RelativeLayout contentLayout;

    private String orderId;
    private String groupId;

    private static final int accept = 1;
    private static final int refuse = 2;

    private static final String PARAM_GROUP_ID = "param.group.id";
    private static final String PARAM_ORDER_ID = "param.order.id";

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_invite_detail;
    }

    @Override
    public String titleName() {
        return getString(R.string.invite_together);
    }

    @Override
    protected void initData() {

        setHeadLeftButtonVisibility(View.GONE);
        groupId = getIntent().getStringExtra(PARAM_GROUP_ID);
        orderId = getIntent().getStringExtra(PARAM_ORDER_ID);

        acceptTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateGroupMemberStatus(accept);
            }
        });

        refuseTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateGroupMemberStatus(refuse);
            }
        });

        getOrderDetail();
    }

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        MaterialDialog.Builder dialog= MaterialDialogUtil.getCommonDialog(mContext, getString(R.string.title), "退出此次邀请",
                getString(R.string.agree), getString(R.string.cancel), new MaterialDialogUtil.OnDialogPoNeClickListener() {
                    @Override
                    public void onPositive() {
                        finish();
                    }

                    @Override
                    public void onNegative() {

                    }
                });

        dialog.show();
    }

    /**获取订单详情
     *
     */
    private void getOrderDetail(){
        Map<String, String> map = new HashMap<>();
        map.put("orderId" , orderId);

        Observable observable = ApiFactory.getEducationApiSingleton().getOrderDetailInfo(SPUtil.stringOut(SPConstant.TOKEN) , map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<InviteOrderResult>(this) {
            @Override
            protected void _onNext(InviteOrderResult orderResult) {
                CourseInfoView.CourseInfoData courseInfoData = new CourseInfoView.CourseInfoData();
                courseInfoData.setContent(orderResult.getOrderInfo().getQuestionIntroduction());

                List<String> imgList = new ArrayList<>();
                if (!Validators.isEmpty(orderResult.getOrderInfo().getQuestionImages())){
                    String[] imgs = orderResult.getOrderInfo().getQuestionImages().split(",");
                    for (int i = 0 ; i < imgs.length ; i++){
                        imgList.add(imgs[i]);
                    }
                }
                courseInfoData.setImgList(imgList);

                courseInfoData.setMemberList(new ArrayList<Member>());

                courseInfoView.bindData(courseInfoData);

                SimpleDateFormat df = new SimpleDateFormat("MM月dd日 HH点mm分");
                teachTimeTv.setText(df.format(new Date(orderResult.getOrderInfo().getAnswerTime())));

                loadingView.setVisibility(View.GONE);
                contentLayout.setVisibility(View.VISIBLE);
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,false);
    }

    /**拒绝或者接受邀请
     *
     * @param groupStatus
     */
    private void updateGroupMemberStatus(int groupStatus){
        Map<String, String> map = new HashMap<>();
        map.put("studentId" , SPUtil.stringOut(SPConstant.USER_ID));
        map.put("groupId" , groupId);
        map.put("groupStatus" , groupStatus + "");

        Observable observable = ApiFactory.getEducationApiSingleton().updateGroupMemberStatus(SPUtil.stringOut(SPConstant.TOKEN) , map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<Void>(this) {
            @Override
            protected void _onNext(Void v) {
                finish();
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,true);
    }
    /**启动该页面
     *
     * @param context
     * @param groupId
     * @param orderId
     */
    public static void startInviteDetailActivity(Context context , String groupId, String orderId){
        Intent intent = new Intent();
        intent.setClass(context , InviteDetailActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(PARAM_GROUP_ID , groupId);
        intent.putExtra(PARAM_ORDER_ID , orderId);
        context.startActivity(intent);
    }
}
